package cn.tedu.carehome.foundation.service.impl;

import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.IllMapper;
import cn.tedu.carehome.foundation.mapper.MedicineMapper;
import cn.tedu.carehome.foundation.pojo.dto.MedicineAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.MedicineUpdateDTO;
import cn.tedu.carehome.foundation.pojo.entity.Medicine;
import cn.tedu.carehome.foundation.pojo.vo.MedicineListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.MedicineStandardVO;
import cn.tedu.carehome.foundation.restful.JsonPage;
import cn.tedu.carehome.foundation.service.IMedicineService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 处理药品业务的实现类
 *
 * @author 程辉
 */
@Service
@Slf4j
public class MedicineServiceImpl implements IMedicineService {
    @Autowired
    private MedicineMapper medicineMapper;

    @Autowired
    private IllMapper illMapper;

    @Override
    public void addNew(MedicineAddNewDTO medicineAddNewDTO) {
        log.debug("开始处理【添加药品】业务，参数：{}", medicineAddNewDTO);

        int countByName = medicineMapper.countByName(medicineAddNewDTO.getName());
        if (countByName > 0) {
            String message = "添加药品失败，药品名称已经被占有！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Medicine medicine = new Medicine();
        BeanUtils.copyProperties(medicineAddNewDTO, medicine);
        log.debug("即将执行插入数据，参数：{}", medicine);
        int rows = medicineMapper.insert(medicine);
        if (rows != 1) {
            String message = "添加药品失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除药品】业务，ID：{}", id);

        MedicineStandardVO standardVO = medicineMapper.getStandardById(id);
        if (standardVO == null) {
            String message = "删除药品失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int count = illMapper.countByMedicineId(id);
        if (count > 0) {
            String message = "删除药品失败，尝试访问的数据与疾病数据存在关联！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        int rows = medicineMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除药品失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(MedicineUpdateDTO medicineUpdateDTO) {
        log.debug("开始处理【根据id修改药品详情】的业务，参数：{}", medicineUpdateDTO);

        MedicineStandardVO standardVO = medicineMapper.getStandardById(medicineUpdateDTO.getId());
        if (standardVO == null) {
            String message = "修改药品详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int count = medicineMapper.countByNameAndNotId(medicineUpdateDTO.getName(), medicineUpdateDTO.getId());
        if (count > 0) {
            String message = "修改药品详情失败，新的药品名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Medicine medicine = new Medicine();
        BeanUtils.copyProperties(medicineUpdateDTO, medicine);
        int rows = medicineMapper.update(medicine);
        if (rows != 1) {
            String message = "修改药品详情失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

    }

    @Override
    public MedicineStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询药品详情】的业务，ID：{}", id);

        MedicineStandardVO standardVO = medicineMapper.getStandardById(id);
        if (standardVO == null) {
            String message = "查询药品详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return standardVO;
    }

    @Override
    public List<MedicineListItemVO> list() {
        log.debug("开始处理【根据id查询药品详情】的业务，无参数");
        List<MedicineListItemVO> list = medicineMapper.list();
        return list;
    }

    @Override
    public JsonPage<MedicineListItemVO> listMedicinesByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<MedicineListItemVO> list = medicineMapper.list();
        return JsonPage.restPage(new PageInfo<>(list));
    }
}
